﻿using ProductMonitor.OpCommand;
using ProductMonitor.UserControls;
using ProductMonitor.ViewModels;
using ProductMonitor.Views;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 初始化视图模型
        /// </summary>
        MainWindowVM mainWindowVM;

        public MainWindow()
        {
            InitializeComponent();

            mainWindowVM = new MainWindowVM();

            //绑定上下文(注：window绑定了vm为上下文后，这里的所有cmd都要进行向上寻找父级)
            this.DataContext = mainWindowVM;
        }

        #region 具体的Command方法

        /// <summary>
        /// 详情点击打开新的机台信息子窗体
        /// </summary>
        private void ShowWorkShopDetailUC()
        {
            //修改属性
            WorkShopDetailUC workShopDetailUC = new WorkShopDetailUC();
            mainWindowVM.ChildUC = workShopDetailUC;

            //设置动画(Animation)效果(由下而上)
            //-->参数(1，2，3) 1-->起始坐标(内的点后边被指定为了margin) 2-->结束时 3(时间，天到毫秒)
            ThicknessAnimation thicknessAnimation = new ThicknessAnimation(new Thickness(0, 50, 0, -50),
                                                        new Thickness(0, 0, 0, 0), new TimeSpan(0, 0, 0, 0, 400));
            //设置透明度
            DoubleAnimation doubleAnimation = new DoubleAnimation(0, 1, new TimeSpan(0, 0, 0, 0, 400));

            //添加属性到窗体之上
            Storyboard.SetTarget(thicknessAnimation, workShopDetailUC);
            Storyboard.SetTarget(doubleAnimation, workShopDetailUC);

            //指定动画效果作用的具体属性
            Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath("Margin"));
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));

            //实例化
            Storyboard storyboard = new Storyboard();

            //将我们设置的动画都加入到“脚本”（集合了一系列的动画）中
            storyboard.Children.Add(thicknessAnimation);
            storyboard.Children.Add(doubleAnimation);
            storyboard.Begin();
        }

        /// <summary>
        /// 返回监视界面
        /// </summary>
        private void BackToMoitorUC()
        {
            MonitorUC monitorUC = new MonitorUC();
            mainWindowVM.ChildUC = monitorUC;
        }

        /// <summary>
        /// 关闭主窗体
        /// </summary>
        private void CloseMainWindow()
        {
            //只是关闭窗口，当前线程并不关闭
            //this.Close();

            //完全关闭，包括所有线程
            Environment.Exit(0);
        }

        /// <summary>
        /// 最小化主窗体
        /// </summary>
        private void MinMainWindow()
        {
            this.WindowState = WindowState.Minimized;

            //最大化
            //this.WindowState = WindowState.Maximized;
        }

        /// <summary>
        /// 最大化主窗体
        /// </summary>
        private void MaxMainWindow()
        {
            this.WindowState = WindowState.Maximized;
        }

        /// <summary>
        /// 打开设置窗体
        /// </summary>
        private void ShowSettings()
        {
            SettingsWin settingsWin = new SettingsWin() { Owner = this };

            settingsWin.ShowDialog();
        }

        #endregion

        #region Command属性

        //打开机台信息
        public Command ShowDetailCmd
        {
            get => new Command(ShowWorkShopDetailUC);
        }

        //返回监视界面
        public Command BackToMonitorUCCmd
        {
            get => new Command(BackToMoitorUC);
        }

        //关闭主窗体
        public Command CloseMainWinCmd
        {
            get => new Command(CloseMainWindow);
        }

        //最小化主窗体
        public Command MinMainWinCmd
        {
            get => new Command(MinMainWindow);
        }

        //最大化主窗体
        public Command MaxMainWinCmd
        {
            get => new Command(MaxMainWindow);
        }

        //打开设置窗体
        public Command ShowSettingsCmd
        {
            get => new Command(ShowSettings);
        }

        #endregion

        //修复窗口最大化后超出屏幕显示区域的问题
        public void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.BorderThickness = new System.Windows.Thickness(8);
            }
            else
            {
                this.BorderThickness = new System.Windows.Thickness(0);
            }
        }
    }
}